package sporteam.web.server;

import javax.jdo.PersistenceManager;

import sporteam.common.PMF;
import sporteam.web.client.AEuser;
import sporteam.web.client.GetUserFromDBService;
import sporteam.web.client.MiniUser;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;

@SuppressWarnings("serial")
public class GetUserFromDBServiceImpl extends RemoteServiceServlet implements
		GetUserFromDBService {

	@Override
	public MiniUser GetUser(String access_token) {
		FacebookClient facebookClient = new DefaultFacebookClient(
				access_token);	//establish connection with facebook-graph API with a specific access_token
		com.restfb.types.User facebook_user = facebookClient.fetchObject("me",
				com.restfb.types.User.class);	//me - the facebook user logged in ATM.
		PersistenceManager pm = PMF.get()
				.getPersistenceManager();
		AEuser user = pm.getObjectById(AEuser.class, facebook_user.getId());
		MiniUser returnMiniUser = new MiniUser(user.GetAge(), user.GetFacebookID(), user.GetFirstName(), user.GetLastName(), user.GetEmail(), user.GetGender(), user.GetWeight()) ;
		return returnMiniUser;
	}

}
